package hongcaosp.app.android.user.member;

import android.view.View;

import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.TestConstants;
import hongcaosp.app.android.login.UserToken;
import hongcaosp.app.android.modle.DataCallBack;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.Pagebean;
import hongcaosp.app.android.modle.bean.PayOrder;
import hongcaosp.app.android.modle.bean.Recharge;
import hongcaosp.app.android.modle.bean.UserInfo;
import hongcaosp.app.android.modle.impl.MemberModleImpl;
import hongcaosp.app.android.modle.impl.UserWalletModelImpl;
import hongcaosp.app.android.modle.mi.MemberModle;
import hongcaosp.app.android.user.settings.wallet.dialog.DialogPayType;
import xlj.lib.android.base.component.BasePresenter;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.DateUtil;

public class MemberDatePresenter extends BasePresenter {
    private IMemberDateView memberDateView;
    private MemberModle memberModle;
    private UserWalletModelImpl userWalletModel;

    //支付方式
    private DialogPayType dialogPayType;
    public long chargeMoneyId = -1;
    private int type = -1;  //0-支付宝 1-微信
    private int operationType = 1;//1-开通会员 2-充值

    public MemberDatePresenter(IMemberDateView memberDateView) {
        this.memberDateView = memberDateView;
        memberModle = new MemberModleImpl();
        this.userWalletModel = new UserWalletModelImpl();
    }

    public void getList() {

        memberModle.rechargeList(UserToken.getDefault().getToken(), 1, new DataCallBack<Pagebean<Recharge>>() {
            @Override
            public void onGetData(Pagebean<Recharge> data) {
                super.onGetData(data);
                List<IMemberDate> memberDates = new ArrayList<>();
                memberDates.addAll(data.getList());
                memberDateView.freshList(memberDates);
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                memberDateView.freshList(null);
            }
        });

    }

    public void getUserData(final long userId) {
        UserInfo user = UserToken.getDefault().getUser();
        String des;
        if (user.isVip()) {
            des = "已开通VIP " + DateUtil.stampToDate(user.getVipExpiration(),"yyyy年MM月dd日 hh:mm") + "到期";
        } else {
            des = "当前未开通VIP";
        }
        memberDateView.updateUser(user,des);
    }

    public void pay() {
        if (dialogPayType == null) {
            dialogPayType = new DialogPayType(memberDateView.getActivity(), new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialogPayType.dismiss();
                    switch (view.getId()) {
                        case R.id.ll_wechat:
                            //微信支付
                            type = 1;
                            wx_pay();
                            break;
                        case R.id.ll_alipay:
                            //支付宝支付
                            type = 0;
                            break;
                    }
                }
            });
        }
        dialogPayType.show();
    }

    private void wx_pay() {
        userWalletModel.openMember(UserToken.getDefault().getToken(), type, operationType, chargeMoneyId, new DataCallBack<PayOrder>() {
            @Override
            public void onGetData(PayOrder data) {
                super.onGetData(data);
                if (data != null) {
                    PayReq payReq = new PayReq();
                    payReq.appId = data.getAppid();
                    payReq.partnerId = data.getPartnerid();
                    payReq.prepayId= data.getPrepayId();
                    payReq.packageValue = "Sign=WXPay";
                    payReq.nonceStr= data.getNonceStr();
                    payReq.timeStamp= data.getTimestamp();
                    payReq.sign= data.getSign();

                    IWXAPI iwxapi = WXAPIFactory.createWXAPI(memberDateView.getActivity(),data.getAppid());
                    iwxapi.sendReq(payReq);
                }
            }

            @Override
            public void onFail(int code, String errorMessage) {
                super.onFail(code, errorMessage);
                ToastManager.getInstance().showToast(errorMessage);
            }
        });
    }
}
